package model

import (
	"gin/common"
	Mysql "gin/database"
	"github.com/Gre-Z/common/jtime"
	"gorm.io/datatypes"
	"strings"
)

type Sentence struct {
	Id            uint64           `json:"id" gorm:"column:id;primaryKey;type:int;autoIncrement"`
	Content       string           `json:"content" gorm:"column:content" binding:"required"`
	Neutral       float64          `json:"neutral" gorm:"column:neutral" binding:"max=128"`
	Positive      float64          `json:"positive" gorm:"column:positive" binding:"max=128"`
	Sentiment     string           `json:"sentiment" gorm:"column:sentiment" binding:"max=128"`
	Negative      float64          `json:"negative" gorm:"column:negative" binding:"max=128"`
	Created       jtime.TstampTime `json:"created" gorm:"column:created;autoCreateTime"`
	Updated       jtime.TstampTime `json:"updated" gorm:"column:updated;autoUpdateTime"`
	NerToken      datatypes.JSON   `json:"ner_token" gorm:"column:ner_token"`
	CustomTag     string           `json:"custom_tag" gorm:"column:custom_tag;default:\"\"" binding:"max=128"`
	Keywords      datatypes.JSON   `json:"keywords" gorm:"column:keywords"`
	IsAnalysis    string           `json:"is_analysis" gorm:"column:is_analysis;default:\"no\""`
	DictId        uint64           `json:"dict_id" gorm:"column:dict_id;default:0"`
	SentenceWords []SentenceWord   `json:"sentence_words" gorm:"foreignKey:sentence_id;references:id"`
}

// 设置表名
func (Sentence) TableName() string {
	return "sentence"
}

func (this *Sentence) Insert() (id uint64, err error) {
	result := Mysql.DB.Create(&this)
	id = this.Id
	if result.Error != nil {
		err = result.Error
		return
	}
	return
}

func (this *Sentence) Find(Param common.SelectQueryParam) (resData Sentence, err error) {

	dictOne := new(Sentence)
	db := Mysql.DB

	// 组装过滤条件
	if Param.Filter.Filter != nil {
		for key, val := range Param.Filter.Filter {
			vals := val.(map[string]interface{})
			if strings.Contains(key, "bw_") {
				db = db.Where(vals["condition"], vals["val1"], vals["val2"])
			} else {
				db = db.Where(vals["condition"], vals["val"])
			}
		}
	} else {
		db = db.Where("?", "1=1")
	}

	// 应用分页
	db = db.Select(Param.Fields)

	result := db.First(&dictOne)
	if result.Error != nil {
		err = result.Error
	}

	return *dictOne, err
}

func (this *Sentence) Select(Param *common.SelectQueryParam) (total int64, resData []Sentence, err error) {

	models := make([]Sentence, 0)
	db := Mysql.DB

	// 组装过滤条件
	if Param.Filter.Filter != nil {
		for key, val := range Param.Filter.Filter {
			vals := val.(map[string]interface{})
			if strings.Contains(key, "bw_") {
				db = db.Where(vals["condition"], vals["val1"], vals["val2"])
			} else {
				db = db.Where(vals["condition"], vals["val"])
			}
		}
	} else {
		db = db.Where("?", "1=1")
	}

	// 获取总条数
	db.Model(&Sentence{}).Count(&total)

	// 查找所有数据

	// 应用分页
	db = db.Select(Param.Fields).Limit(Param.Limit).Offset(Param.Offset)

	// 应用排序参数
	if Param.Order != "" {
		db = db.Order(Param.Order)
	}

	result := db.Find(&models)
	if result.Error != nil {
		err = result.Error
		return
	}

	return total, models, err
}

func (this *Sentence) FindById(id uint64) (resData Sentence, err error) {

	db := Mysql.DB

	db = db.Where("id = ?", id)

	// 应用分页
	db = db.Select("*")

	result := db.First(&resData)
	if result.Error != nil {
		err = result.Error
		return resData, err
	}

	return resData, err
}

// 更新dict
func (this *Sentence) Update(sentence Sentence) (Sentence, error) {
	db := Mysql.DB
	result := db.Updates(&sentence)
	if result.Error != nil {
		return sentence, result.Error
	}
	return this.FindById(sentence.Id)
}

func (this *Sentence) Delete(dict Sentence) {
	Mysql.DB.Delete(&dict)
}

// 批量删除
func (this *Sentence) DeleteAll(where string) {
	Mysql.DB.Where(where).Delete(&Sentence{})
	return
}

func (this *Sentence) InsertAll(sentences *[]Sentence) {
	Mysql.DB.Create(&*sentences)
	return
}

// 批量删除
func (this *Sentence) DeleteByFilter(param *common.Filter) {
	db := Mysql.DB

	if param.Filter == nil {
		return
	}
	// 组装过滤条件

	for key, val := range param.Filter {
		vals := val.(map[string]interface{})
		if strings.Contains(key, "bw_") {
			db = db.Where(vals["condition"], vals["val1"], vals["val2"])
		} else {
			db = db.Where(vals["condition"], vals["val"])
		}
	}
	db.Delete(Sentence{})

}
